package com.neu.api.service.impl;

import com.neu.api.dto.CustomerDTO;
import com.neu.api.mapper.CustomerMapper;
import com.neu.api.pojo.Customer;
import com.neu.api.service.CustomerService;
import com.neu.api.service.CustomerService;
import com.neu.api.utils.PageResult;
import com.neu.api.vo.CustomerVO;
import com.neu.api.vo.CustomerVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.util.Date;

@Service
@Slf4j
public class CustomerServiceImpl implements CustomerService {

    @Value("${neu.password}")
    private String password;
    //private String password = "123456";

    @Value("${neu.salt}")
    private String salt;


    @Resource
    private CustomerMapper customerMapper;


    /**
     * 用户登录
     * @param phone
     * @param password
     * @return
     */
    @Override
    public CustomerVO login(String phone, String password) {
        //密码加密
        password = DigestUtils.md5DigestAsHex((password+salt).getBytes());
        return customerMapper.login(phone,password);
    }

    @Override
    public void add(CustomerDTO customerDTO) {
        Customer customer = new Customer();
        BeanUtils.copyProperties(customerDTO,customer);

        customer.setCreateTime(new Date());
        customerMapper.add(customer);
    }


    @Override
    public void update(Long id, CustomerDTO customerDTO) {
        Customer customer = new Customer();
        BeanUtils.copyProperties(customerDTO,customer);
        customer.setId(id);
        customer.setCreateTime(new Date());
        customerMapper.update(customer);
    }

    @Override
    public void deleteById(long id) {
        customerMapper.deleteById(id);
    }


}
